package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.domain.view.CourseEnroll;
import com.learn.core.domain.view.UserDetailsGather;
import com.learn.core.dto.LCourseDTO;
import com.learn.core.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LGraduationController
 * @date 2021.05.11 14:15
 * @describe 结业管理
 */
@Api("结业管理")
@RestController
@RequestMapping("/manager/graduation")
public class LGraduationController {

    @Autowired
    private ICourseEnrollService iCourseEnrollService;
    @Autowired
    private IUserDetailsGatherService iUserDetailsGatherService;
    @Autowired
    private ILEnrollService ilEnrollService;
    @Autowired
    private ILCourseService iLCourseService;
    @Autowired
    private ILChapterService ilChapterService;
    @Autowired
    private ILCoursewareService ilCoursewareService;
    @Autowired
    private ILLearnService ilLearnService;
    @Autowired
    private ILTeacherService ilTeacherService;
    @Autowired
    private ILOrganizationService ilOrganizationService;
    @Autowired
    private ILThemeService ilThemeService;

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //获取结业管理列表
    @ApiOperation(value = "获取结业管理列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<CourseEnroll> list(@RequestBody LCourseDTO lCourseDTO, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lCourseDTO==null||lCourseDTO.getPage()==null||lCourseDTO.getRows()==null){
                return new PageResult<CourseEnroll>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCourseDTO.getPage(),lCourseDTO.getRows());
            QueryWrapper<CourseEnroll> queryWrapper = new QueryWrapper<CourseEnroll>();
            if(lCourseDTO.getLikeParameter() != null)
                queryWrapper.like("course_name",lCourseDTO.getLikeParameter());
            if(lCourseDTO.getCourseSortId()!=null)
                queryWrapper.eq("course_sort_id",lCourseDTO.getCourseSortId());
            queryWrapper.eq("stutes", "3");
            if(lCourseDTO.getSortType()!=null&&lCourseDTO.getSortBy()!=null)
                queryWrapper.orderBy(true,lCourseDTO.getSortType(),lCourseDTO.getSortBy());
            Page<CourseEnroll> courseEnrolls = (Page<CourseEnroll>) iCourseEnrollService.list(queryWrapper);
            if (CollectionUtils.isEmpty(courseEnrolls)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (CourseEnroll courseEnroll: courseEnrolls) {
                QueryWrapper<UserDetailsGather> gatherQueryWrapper = new QueryWrapper<UserDetailsGather>();
                gatherQueryWrapper.eq("user_id" ,courseEnroll.getUserId());
                UserDetailsGather lUser = iUserDetailsGatherService.getOne(gatherQueryWrapper);
                if(null != lUser)
                    courseEnroll.setUserName(lUser.getName());
                LTeacher lTeacher = ilTeacherService.getById(courseEnroll.getTeacher());
                if(null != lTeacher)
                    courseEnroll.setTeacherName(lTeacher.getTeacherName());
                LOrganization lOrganization = ilOrganizationService.getById(courseEnroll.getCourseCompany());
                if(null != lOrganization)
                    courseEnroll.setCourseCompanyName(lOrganization.getOrganizationName());
                LTheme lTheme = ilThemeService.getById(courseEnroll.getCourseTheme());
                if(null != lTheme)
                    courseEnroll.setCourseThemeName(lTheme.getThemeName());
            }
            // 返回
            return new PageResult(courseEnrolls.getTotal(), courseEnrolls, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //通过结业审核
    @ApiOperation(value = "通过结业审核")
    @GetMapping("/passed")
    @UserLoginToken
    @Log(title = "发布考次", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> passed(@RequestParam("id")Integer id, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if (StringUtils.isEmpty(id)) {
                return (CommonResult) ResultUtil.error(ResultEnum.LOAD_ERROR);
            }
            LEnroll lEnroll = new LEnroll();
            lEnroll.setEnrollId(id);
            lEnroll.setStutes("4");
            lEnroll.setUpdateTime(new Date());
            lEnroll.setUpdateUser(userId);
            if(ilEnrollService.updateById(lEnroll)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //课程详情
    @ApiOperation(value = "课程详情")
    @UserLoginToken
    @GetMapping("/courseInfo")
    public CommonResult<Object> info( @RequestParam("id")String courseId,@RequestParam("userId")String userId){
        try {
            if (courseId==null){
                return (CommonResult) ResultUtil.error(ResultEnum.LOAD_ERROR);
            }
            QueryWrapper<LCourse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("course_id",courseId);
            LCourse lCourse = iLCourseService.getOne(queryWrapper);
            if(null != lCourse){
                QueryWrapper<LChapter> lChapterQueryWrapper = new QueryWrapper<>();
                lChapterQueryWrapper.eq("course_id",lCourse.getCourseId());
                List<LChapter> lChapterList = ilChapterService.list(lChapterQueryWrapper);
                for (LChapter lChapter : lChapterList) {
                    LCourseware lCourseware = ilCoursewareService.getById(lChapter.getCoursewareId());
                    lChapter.setlCourseware(lCourseware);
                    QueryWrapper<LLearn> learnQueryWrapper = new QueryWrapper<>();
                    learnQueryWrapper.eq("user_id",userId);
                    learnQueryWrapper.eq("chapter_id",lChapter.getChapterId());
                    learnQueryWrapper.eq("courseware_id",lChapter.getCoursewareId());
                    LLearn learn = ilLearnService.getOne(learnQueryWrapper);
                    lChapter.setlLearn(learn);
                }
                lCourse.setlChapters(lChapterList);
            }
            // 返回
            return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS,lCourse);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
    }
}
